/*
 * @Author: 胡海
 * @Date: 2020-02-21 18:23:26
 * @LastEditors: 胡海
 * @LastEditTime: 2020-03-11 19:58:29
 * @Description: 
 */
const bcrypt = require('bcryptjs')

const {
    sequelize
} = require('../../core/db.js')


const {
    Sequelize,
    Model,
    Op
} = require('sequelize')

const {
    isOptionalObj
} = require('../../app/libs/util')

// define
class User extends Model {
    static async verifyUserPassword(username, plainPassword) { //登录
        const user = await User.findOne({
            where: {
                username
            }
        })
        if (!user) {
            throw new global.errs.AuthFailed('账号不存在')
        }
        // user.password === plainPassword
        const correct = bcrypt.compareSync(
            plainPassword, user.password)
        if (!correct) {
            throw new global.errs.AuthFailed('密码不正确')
        }
        return user
    }

    static async editPassword(ctx, v) { //修改密码
        let password = v.get('body.password1')
        const user = await User.update({
            password
        }, {
            where: {
                id: v.get('body.id') || ctx.auth.uid
            }
        })
        throw new global.errs.Success()
    }

    static async upRegister(ctx, v) { //修改用户
        let request = {
            username: v.get('body.username'),
            nickname: v.get('body.nickname'),
            avatar: v.get('body.avatar'),
            type: v.get('body.type'),
        }
        let data = isOptionalObj(request)
        const user = await User.update(data, {
            where: {
                id: v.get('body.id') || ctx.auth.uid
            }
        })
        throw new global.errs.Success()
    }
    static async deRegister(ctx, v) { //删除用户
        let request = {
            id: v.get('body.id'),
        }
        const p = await User.destroy({
            where: {
                id: request.id
            }
        })
        throw new global.errs.Success()
    }

    static async getUserList(ctx,v) { // 用户列表
        let request = {
            start: v.get('body.start'),
            count: v.get('body.count'),
            p: v.get('body.p'),
            type: v.get('body.type'),
        }
        const total = await User.count()
        const userList = await User.findAll({
            where: {
                username: {
                    [Op.like]: `%${request.p}%`
                },
                type: {
                    [Op.like]: `%${request.type}%`
                },
            },
            order: [
                ['id', 'desc']
            ],
            limit: request.count,
            offset: request.start,
        })
        const data = {
            list: userList,
            total
        }
        return data
    }
}

User.init({
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    username: Sequelize.STRING,
    nickname: Sequelize.STRING,
    avatar: Sequelize.STRING,
    type: Sequelize.INTEGER,
    password: {
        //扩展 设计模式 观察者模式
        //ES6 Reflect Vue3.0 
        //密码加密
        type: Sequelize.STRING,
        set(val) {
            const salt = bcrypt.genSaltSync(10)
            const psw = bcrypt.hashSync(val, salt)
            this.setDataValue('password', psw)
        }
    },
}, {
    sequelize,
    tableName: 'user',
    alter: true
})

module.exports = {
    User
}

// 数据迁移 SQL 更新 风险